package com.lanyou.esb.redis.proxy.base;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;

import redis.clients.jedis.Jedis;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.lanyou.esb.redis.JedisPoolUtils;
import com.lanyou.esb.redis.proxy.SetJedisProxy;

/**
 * Set数据类型,操作Redis基础代理类
 * 
 * @author Davey.wu
 */
public abstract class BaseSetJedisProxy<T> extends BaseJedisProxy<T> implements
		SetJedisProxy<T> {
	private String setKey;

	@Override
	public void sadd(String key, T... members) throws JsonProcessingException {
		if (members == null || members.length == 0) {
			return;
		}

		List<String> jsons = new ArrayList<>(members.length);
		for (T t : members) {
			jsons.add(this.toJson(t));
		}
		Jedis jedis = JedisPoolUtils.getJedis();
		if (jedis == null) {
			return;
		}
		boolean broken = false;
		try {
			jedis.sadd(key, jsons.toArray(new String[jsons.size()]));
		} catch (Exception e) {
			broken = true;
		} finally {
			JedisPoolUtils.returnRes(jedis, broken);
		}
	}

	@Override
	public void saddForString(String key, String... members)
			throws JsonProcessingException {
		if (members == null || members.length == 0) {
			return;
		}
		Jedis jedis = JedisPoolUtils.getJedis();
		if (jedis == null) {
			return;
		}
		boolean broken = false;
		try {
			jedis.sadd(key, members);
		} catch (Exception e) {
			broken = true;
		} finally {
			JedisPoolUtils.returnRes(jedis, broken);
		}
	}

	@Override
	public List<T> smembers(String key) throws IOException {
		Set<String> set = null;
		Jedis jedis = JedisPoolUtils.getJedis();
		if (jedis == null) {
			return null;
		}
		boolean broken = false;
		try {
			set = jedis.smembers(key);
		} catch (Exception e) {
			broken = true;
		} finally {
			JedisPoolUtils.returnRes(jedis, broken);
		}
		if (set == null || set.isEmpty()) {
			return null;
		}
		List<T> objects = new ArrayList<>(set.size());
		for (String s : set) {
			objects.add(this.fromJson(s));
		}
		return objects;
	}

	@Override
	public List<String> smembersForString(String key) throws IOException {
		Set<String> set = null;
		Jedis jedis = JedisPoolUtils.getJedis();
		if (jedis == null) {
			return null;
		}
		boolean broken = false;
		try {
			set = jedis.smembers(key);
		} catch (Exception e) {
			broken = true;
		} finally {
			JedisPoolUtils.returnRes(jedis, broken);
		}
		if (set == null || set.isEmpty()) {
			return null;
		}
		List<String> objects = new ArrayList<>(set.size());
		for (String s : set) {
			objects.add(s);
		}
		return objects;
	}

	@Override
	public T spop(String key) throws JsonParseException, JsonMappingException,
			IOException {
		String result = "";
		Jedis jedis = JedisPoolUtils.getJedis();
		if (jedis == null) {
			return null;
		}
		boolean broken = false;
		try {
			result = jedis.spop(key);
		} catch (Exception e) {
			broken = true;
		} finally {
			JedisPoolUtils.returnRes(jedis, broken);
		}
		if (StringUtils.isBlank(result)) {
			return null;
		}
		return this.fromJson(result);
	}

	@Override
	public String spopForString(String key) throws JsonParseException,
			JsonMappingException, IOException {
		String result = "";
		Jedis jedis = JedisPoolUtils.getJedis();
		if (jedis == null) {
			return null;
		}
		boolean broken = false;
		try {
			result = jedis.spop(key);
		} catch (Exception e) {
			broken = true;
		} finally {
			JedisPoolUtils.returnRes(jedis, broken);
		}
		return result;
	}

	public void setSetKey(String setKey) {
		this.setKey = setKey;
	}

	@Override
	public String getSetKey() {
		return setKey;
	}

	@Override
	public String generateKeyFromObject(T t) {
		return setKey;
	}

}
